<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/jquery-3.6.0.js"></script>
		<style>
			* {
				margin: 0px;
				padding: 0px;
			}

			a {
				text-decoration: none;
			}

			.box {
				background-color: #F0F8FF;
				border: #fffcf8 solid 2px;
			}

			.cart-header {
				height: 30px;
				border: #0000FF solid 1px;
				line-height: 30px;
				margin: 5px;
				padding: 5px;
				background-color: #f4f8ff;
			}

			.cart-header div {
				float: left;
			}

			.t-checkbox {
				width: 50px;
			}

			.t-goods {
				width: 400px;
				text-align: center;
			}

			.t-price {
				width: 150px;
				text-align: center;
			}

			.t-num {
				width: 150px;
				text-align: center;
			}

			.t-sum {
				width: 150px;
				text-align: center;
			}

			.t-action {
				width: 150px;
				text-align: center;
			}

			.cart-item {
				height: 100px;
				border: #0000FF solid 1px;
				padding: 10px;
				margin: 5px;
			}

			.p-checkbox {
				width: 50px;
				float: left;
			}

			.p-goods {
				width: 400px;
				float: left;
			}

			.p-img {
				float: left;
				padding: 5px;
			}

			.p-msg {
				float: left;
				width: 200px;
			}

			.p-price {
				width: 150px;
				float: left;
				text-align: center;
			}

			.p-num {
				float: left;
				width: 150px;
				text-align: center;
			}

			.inc,
			.dec {
				display: inline-block;
				height: 10px;
				line-height: 10px;
				width: 10px;
			}

			.txt {
				display: inline-block;
				width: 50px;
			}

			.p-sum {
				width: 150px;
				float: left;
				text-align: center;
			}

			.p-action {
				width: 150px;
				float: left;
				text-align: center;
				sss
			}

			.cur-goods {
				background-color: #FFE4C4;
			}

			.cart-stat {
				height: 50px;
				border: #0000FF solid 1px;
				line-height: 50px;
				margin: 5px;
			}

			.select-all {
				float: left;
				height: 20px;
				line-height: 20px;
				padding: 10px;

			}

			.del {
				float: left;
				width: 200px;
			}

			.del a {
				padding-left: 5px;
				height: 20px;
				line-height: 20px;
			}

			.stat-right {
				float: right;
			}

			.selected-goods {
				float: left;
				margin-right: 10px;
			}

			.selected-goods span {
				padding: 0 5px;
				color: #A52A2A;
				height: 50px;
				line-height: 50px;
			}

			.price-sum {
				float: left;
				margin-right: 10px;
				height: 50px;
				line-height: 50px;
			}

			.settlement {
				float: left;
				margin-right: 20px;
				height: 50px;
				line-height: 50px;
			}

			.cur {
				background-color: orange;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="cart-header">
				<div class="t-checkbox">
					<input type="checkbox" class="checkall" />全选
				</div>
				<div class="t-goods">商品</div>
				<div class="t-price">单价</div>
				<div class="t-num">数量</div>
				<div class="t-sum">小计</div>
				<div class="t-action">操作</div>
			</div>

			<div class="cart-item-list">
				<div class="cart-item">
					<div class="p-checkbox">
						<input type="checkbox" class="j-checkall" />
					</div>
					<div class="p-goods">
						<div class="p-img">
							<img src="" />
						</div>
						<div class="p-msg">手机</div>
					</div>
					<div class="p-price">￥1000.00</div>
					<div class="p-num">
						<div>
							<a href="#" class="dec">-</a>
							<input type="text" class="txt" value="1" />
							<a href="#" class="inc">+</a>
						</div>
					</div>
					<div class="p-sum">￥1000.00</div>
					<div class="p-action"><a href="#">删除</a></div>
				</div>
				<div class="cart-item">
					<div class="p-checkbox">
						<input type="checkbox" class="j-checkall" />
					</div>
					<div class="p-goods">
						<div class="p-img">
							<img src="" />
						</div>
						<div class="p-msg">手机</div>
					</div>
					<div class="p-price">￥1000.00</div>
					<div class="p-num">
						<div>
							<a href="#" class="dec">-</a>
							<input type="text" class="txt" value="1" />
							<a href="#" class="inc">+</a>
						</div>
					</div>
					<div class="p-sum">￥1000.00</div>
					<div class="p-action"><a href="#">删除</a></div>
				</div>
			</div>
			<!--统计-->
			<div class="cart-stat">
				<div class="select-all">
					<input type="checkbox" class="checkall" />全选
				</div>
				<div class="del">
					<a href="#">删除</a>
					<a href="#">清理购物车</a>
				</div>
				<div class="stat-right">
					<div class="selected-goods">已选<span>0</span>件商品</div>
					<div class="price-sum">总价<span>￥1000.00</span></div>
					<div class="settlement"><a herf="#">去结算</a></div>
				</div>
			</div>
			<script type="text/javascript">
				/* function setBgc(val) {
					if (val.checked) {
						val.parentElement.parentElement.className = "cart-item cur-goods";
					} else {
						val.parentElement.parentElement.className = "cart-item";
					}
				}

				let checkall = document.querySelector(".checkall");
				checkall.addEventListener("click", function() {
					let jCheckall = document.querySelectorAll(".j-checkall");
					jCheckall.forEach(function(val) {
						val.checked = checkall.checked;
						setBgc(val);

					})
				}) */

				//全选
				$('.checkall').click(function() {
					$('.j-checkall').prop('checked', $(this).prop('checked'));
					if ($(this).prop('checked')) {
						$('.j-checkall').parents('.cart-item').addClass('cur-goods');
					} else {
						$('.j-checkall').parents('.cart-item').removeClass('cur-goods');
					}

					getSum();
				})

				//单选
				$('.j-checkall').click(function() {
					if ($('.j-checkall:checked').length == $('.j-checkall').length) {
						$('.checkall').prop('checked', true);
					} else {
						$('.checkall').prop('checked', false);
					}

					if ($(this).prop('checked')) {
						$(this).parents('.cart-item').addClass('cur-goods');
					} else {
						$(this).parents('.cart-item').removeClass('cur-goods');
					}

					getSum();
				})
				//增加购买数量
				$('.inc').click(function() {
					//获取数量
					let txtVal = $(this).siblings('.txt').val();
					txtVal++;
					//++后重写放到input中
					$(this).siblings('.txt').val(txtVal);

					//获取单价
					let pPrice = $(this).parents('.p-num').siblings('.p-price').html();
					pPrice = pPrice.substring(1);
					//计算小计
					let pSum = (pPrice * txtVal).toFixed(2);
					//设置小计

					$(this).parents('.p-num').siblings('.p-sum').html("￥" + pSum);

					getSum();
				})
				//减少购买数量
				$('.dec').click(function() {
					//获取数量
					let txtVal = $(this).siblings('.txt').val();
					txtVal--;
					if (txtVal >= 0) {
						//++后重写放到input中
						$(this).siblings('.txt').val(txtVal);

						//获取单价
						let pPrice = $(this).parents('.p-num').siblings('.p-price').html();
						pPrice = pPrice.substring(1);
						//计算小计
						let pSum = (pPrice * txtVal).toFixed(2);
						//设置小计

						$(this).parents('.p-num').siblings('.p-sum').html("￥" + pSum);
					}

					getSum();

				})

				//删除
				$('.p-action').children().click(function(e){
					//阻止默认行为
					e.preventDefault();
					$(this).parents('.cart-item').remove();
					getSum();
				})
				
				//手动修改购买数量
				$('.txt').change(function() {
					//获取数量
					let txtVal = $(this).val();
					//获取单价
					let pPrice = $(this).parents('.p-num').siblings('.p-price').html();
					pPrice = pPrice.substring(1);
					//计算小计
					let pSum = (pPrice * txtVal).toFixed(2);
					//设置小计
				
					$(this).parents('.p-num').siblings('.p-sum').html("￥" + pSum);
				
					getSum();
				})
				
				function getSum() {
					let totalNum = 0;
					let totalMoney = 0;

					$('.j-checkall:checked').each(function(k, v) {
						let pNum = $(v).parents('.p-checkbox').siblings('.p-num').children().children('.txt').val();
						totalNum += parseInt(pNum);

						let pSum = $(v).parents('.p-checkbox').siblings('.p-sum').html().substring(1);
						totalMoney += parseFloat(pSum);
					})

					$('.selected-goods').children().html(totalNum);
					$('.price-sum').children().html("￥" + totalMoney.toFixed(2));
				}
			</script>
		</div>
	</body>
</html>
